package com.cloudptt.wp.ui.views;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.comm.Common;
import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.adapter.GCallEventAdapter;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.dlg.UiDlg;
import com.cloudptt.wp.ui.message.component.AudioRecorderButton;
import com.cloudptt.wp.ui.views.listener.CallSysDlgListener;
import com.cloudptt.wp.ui.views.listener.EditLyChangeListener;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.SessionInfo;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class EditLayout extends LinearLayout implements OnClickListener, CallSysDlgListener, View.OnLongClickListener,DialogInterface.OnClickListener {

	public static final String TAG = "EditLayout";
	private static final int MAX_CONTENT_LEN = 300;
	public static final int SHOW_EDIT = 0;
	public static final int SHOW_PTT =1;

	private Context context = null;
	//	private LinearLayout editLayout;

//	private ImageView switchToPttIV;
	private ImageView switchIv;
	private boolean isVoice = false;
//	private ImageView switchToEditIV;

	private ImageView sendTextBtn;
	private TextView txtCountTv;

	private EditText contentEditText;

	private AudioRecorderButton audioRecorderButton;

	private SessionMenuLayout2 mExpandFuncLayout;
	private InputMethodManager imm;
//	private ImageView mAddFuncIv;
	private ImageView switchPttIv;

	private boolean isShowExpand = false;
	//	private GCallEventAdapter mCallEventAdapter = null;

	private AddrInfo addrInfo = null;
	private EditLyChangeListener listener = null;

	public EditLayout(Context context) {
		this(context, null);
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public EditLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public EditLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		this(context, attrs, defStyleAttr, 0);
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public EditLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub
		this.context = context;

		LayoutInflater.from(context).inflate(R.layout.bottom_edit_layout, this);

		//		editLayout = (LinearLayout)findViewById(R.id.bottom_edit_ll);
		mExpandFuncLayout = (SessionMenuLayout2)findViewById(R.id.expand_func_layout);


//		switchToPttIV = (ImageView)findViewById(R.id.switch_ptt);
//		switchToPttIV.setOnClickListener(this);

		switchIv = (ImageView)findViewById(R.id.switchIv);
		switchIv.setOnClickListener(this);

//		switchToEditIV = (ImageView)findViewById(R.id.switch_edit);
//		switchToEditIV.setOnClickListener(this);

		sendTextBtn=(ImageView) findViewById(R.id.send_text_msg);
		sendTextBtn.setOnClickListener(this);

		txtCountTv=(TextView) findViewById(R.id.txtCountTv);

//		mAddFuncIv = (ImageView)findViewById(R.id.add_func);
//		mAddFuncIv.setOnClickListener(this);

		switchPttIv = (ImageView)findViewById(R.id.switchPttIv);
		switchPttIv.setOnClickListener(this);

		contentEditText=(EditText) findViewById(R.id.contentEditText);
		contentEditText.setOnLongClickListener(this);
		//contentEditText.requestFocus();

		audioRecorderButton=(AudioRecorderButton)findViewById(R.id.record_button);
		audioRecorderButton.setAudioFinishRecorderListener(new AudioRecorderButton.AudioFinishRecorderListener() {
			@Override
			public void onFinish(long sec, String filePath) {
				if(listener != null)
				{
					Log.d(TAG, "audioRecorderButton onFinish sec = " + sec + " filePath = " + filePath);
					listener.uploadVoiceMsg(sec,filePath);
				}
			}
		});


		imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);  
		contentEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean isFocus) {
				// TODO Auto-generated method stub
				Log.d(TAG, "isEdittext focus = "+isFocus);
				if(isFocus){
					imm.showSoftInputFromInputMethod(contentEditText.getWindowToken(), 0);
//					if(mExpandFuncLayout.isShown()){
//						mExpandFuncLayout.setVisibility(View.GONE);
//					}

				}else{
					imm.hideSoftInputFromWindow(contentEditText.getWindowToken(), 0); 
				}
			}
		});
		contentEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				String str = contentEditText.getText().toString();
				Log.d(TAG, "isEdittext str.length() = "+str.length());
				if(str.length() > MAX_CONTENT_LEN) {
					str = Common.substringByBytes(str,MAX_CONTENT_LEN);
					Log.d(TAG, "addTextChangedListener str = " + str);
					contentEditText.setText(str);
					contentEditText.setSelection(str.length());
					CommDlg.showToast(context,R.string.msg_content_err);
					return;
				}
				if(str != null
						&& Common.getStrCount(str) > 0)
				{
					switchIv.setVisibility(View.VISIBLE);
					switchPttIv.setVisibility(View.GONE);
					sendTextBtn.setVisibility(View.VISIBLE);
					txtCountTv.setText(str.length()+"/"+MAX_CONTENT_LEN);
					txtCountTv.setVisibility(View.VISIBLE);
				}
				else
				{
					switchIv.setVisibility(View.VISIBLE);
					switchPttIv.setVisibility(View.VISIBLE);
					sendTextBtn.setVisibility(View.GONE);
					txtCountTv.setVisibility(View.GONE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void setListener(AddrInfo addrInfo,EditLyChangeListener listener) {
		this.addrInfo = addrInfo;
		this.listener = listener;
		if(mExpandFuncLayout != null)
		{
			mExpandFuncLayout.setListener(addrInfo,this);
		}
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		SysApplication app = ((SysApplication) context.getApplicationContext());
		Product product = app.getProduct();

		switch (view.getId()) {
//		case R.id.switch_ptt:
//			if(listener != null)
//			{
//				listener.onSwitchPttLy();
//			}
//			break;
		case R.id.switchIv:
			isVoice = !isVoice;
			if(isVoice) {
				contentEditText.setVisibility(View.GONE);
				switchIv.setVisibility(View.VISIBLE);
				switchIv.setBackgroundResource(R.drawable.msg_switch_to_edit);
//			switchToEditIV.setVisibility(View.VISIBLE);
				audioRecorderButton.setVisibility(View.VISIBLE);
			}
			else
			{
				contentEditText.setVisibility(View.VISIBLE);
				contentEditText.requestFocus();
				imm.showSoftInput(contentEditText, 0);
				switchIv.setVisibility(View.VISIBLE);
				switchIv.setBackgroundResource(R.drawable.msg_switch_to_record);

//				switchToEditIV.setVisibility(View.GONE);
				audioRecorderButton.setVisibility(View.GONE);
			}
			break;

//		case R.id.switch_edit:
//			contentEditText.setVisibility(View.VISIBLE);
//			contentEditText.requestFocus();
//			imm.showSoftInput(contentEditText, 0);
//			switchToRecordIV.setVisibility(View.VISIBLE);
//
//			switchToEditIV.setVisibility(View.GONE);
//			audioRecorderButton.setVisibility(View.GONE);
//			break;

		case R.id.send_text_msg:
			String contentStr = contentEditText.getText().toString();
			int len = contentStr.length();
			if (len <= 0
					|| MAX_CONTENT_LEN < len)
			{
				CommDlg.showToast(context, getResources().getString(R.string.msg_content_err));
				return;
			}
			if(listener != null)
			{
				listener.onSendMsg(SessionInfo.ContentType_Text,contentStr);
			}
			contentEditText.setText("");
			contentEditText.requestFocus();
			break;
		case R.id.switchPttIv:
			break;

//		case R.id.add_func:
//			isShowExpand = !isShowExpand;
//			showExpandFunc(isShowExpand);
//			break;


//		case R.id.choose_pic_iv:
//			listener.doAction(CHOOSE_PIC);
//			break;
//
//		case R.id.camera_iv:
//			listener.doAction(OPEN_CAMERA);
//			break;
//
//		case R.id.videoPush:
//			product.getPCallEventAdapter().makePrivateCall((UserInfo)addrInfo, CallAttr.CallType_Push_One_To_One,true,false,"");
//			break;
//		case R.id.videoPull:
//			product.getPCallEventAdapter().makePrivateCall((UserInfo)addrInfo, CallAttr.CallType_Pull_One_To_One,true,false,"");
//			break;
//		case R.id.call_iv:
//			product.getPCallEventAdapter().makePrivateCall((UserInfo)addrInfo, CallAttr.CallType_Pull_One_To_One,true,false,"");
//			break;
//		case R.id.conference_iv:
////			product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Conference,false);
//			product.getPCallEventAdapter().makePrivateCall((UserInfo)addrInfo, CallAttr.CallType_Pull_One_To_One,false,true,"");
//			break;
		default:
			break;
		}
	}
	@Override
	public void onCallSysDlg(int type)
	{
		Log.debug(this.getClass(),"onCallSysDlg type = " + type);
		if(listener != null) {
			listener.doAction(type);
		}
	}
	@Override
	public void onDoFun(int viewId)
	{
		Log.debug(this.getClass(),"onDoFun viewId = " + viewId);
		SysApplication app = ((SysApplication) context.getApplicationContext());
		Product product = app.getProduct();

		switch (viewId) {
			case R.id.funcFile:
//			product.getPCallEventAdapter().makePrivateCall((UserInfo)addrInfo, CallAttr.CallType_Pull_One_To_One,true,false,"");
//				product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Ambient_Listening,false,false,"");
				break;
			case R.id.funcLocal:
//			product.getPCallEventAdapter().makePrivateCall((UserInfo)addrInfo, CallAttr.CallType_Pull_One_To_One,true,false,"");
//				product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Ambient_Viewing,true,false,"");
				break;
		case R.id.funcVoice:
		case R.id.voiceCallIv:
			product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Private,false,false,"");
			break;
		case R.id.funcVideo:
		case R.id.videoCallIv:
//			product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Conference,false);
			if(addrInfo != null
				&& addrInfo.isGroupType())
			{
				product.setCurGroupInfo(addrInfo);
				GCallEventAdapter callEventAdapter = product.getGCallEventAdapter();
				if(callEventAdapter != null){
					callEventAdapter.onPtt(Const.MSG_TALK_ON, true,true,true,false);
				}
			}
			else {
				product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Private, true, false, "");
			}
			break;
		case R.id.funcVideoPush:
			product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Push_One_To_One,true,false,"");
			break;
		case R.id.funcVideoPull:
			product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Pull_One_To_One,true,false,"");
			break;
			default:
				break;
		}
	}

	public void clearState(){
		showExpandFunc(false);
		contentEditText.clearFocus();
	}

	private void showExpandFunc(boolean isShow)
	{
		if(isShow)
		{
			contentEditText.clearFocus();

		}
		else
		{
			contentEditText.requestFocus();
		}
	}

	@Override
	public boolean onLongClick(View view) {
		String item1 = context.getString(R.string.session_paste);
		String[] list = new String[]{item1};
		UiDlg.showListDialog(context, list, this);
		return false;
	}
	@Override
	public void onClick(DialogInterface dialogInterface, int index)
	{
		switch (index) {
			case 0:
				doCopyAction();
				break;
			default:
				break;
		}
	}
	private void doCopyAction()
	{
		ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
		if (clipboard.hasPrimaryClip()) {
			ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
			contentEditText.getText().insert(contentEditText.getSelectionStart(), item.getText());
		}
	}
//	public void setSwitchPTTEnable(boolean isPTTenable){
//		switchToPttIV.setVisibility(View.GONE);
//	}
}
